package com.itheima.reggie.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.common.UserHolder;
import com.itheima.reggie.domain.*;
import com.itheima.reggie.mapper.AddressMapper;
import com.itheima.reggie.mapper.OrderDetailMapper;
import com.itheima.reggie.mapper.OrderMapper;
import com.itheima.reggie.service.CartService;
import com.itheima.reggie.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class OrderServiceImpl implements OrderService {

    //订单
    @Autowired
    private OrderMapper orderMapper;
    //订单明细
    @Autowired
    private OrderDetailMapper orderDetailMapper;
    //收货地址
    @Autowired
    private AddressMapper addressMapper;
    //获取购物车注入
    @Autowired
    private CartService cartService;


    //用户下单
    @Override
    public void submit(Order order) {
        //获取登陆人
        User user = UserHolder.get();
        if (user == null) {
            throw new CustomException("用户未登录，不能下单");
        }
        //查询收货地址对象
        Address address = addressMapper.selectById(order.getAddressId());
        if (address == null) {
            throw new CustomException("收货地址为空，不能下单");
        }
        //获取购物车
        List<Cart> cartList = cartService.findList();
        if (cartList == null && cartList.size() == 0) {
            throw new CustomException("购物车为空，不能下单");
        }
        //通过mp实现雪花id
        Long orderId = IdWorker.getId();
        //订单总金额
        BigDecimal orderAmount = new BigDecimal(0);
        for (Cart cart : cartList) {
            //封装订单明细
            OrderDetail orderDetail = new OrderDetail();
            orderDetail.setName(cart.getName());//菜品名称
            orderDetail.setOrderId(orderId);//订单id
            orderDetail.setDishId(cart.getDishId());//菜品id
            orderDetail.setSetmealId(cart.getSetmealId());//套餐id
            orderDetail.setAmount(cart.getAmount()); // 单价
            orderDetail.setDishFlavor(cart.getDishFlavor());//口味
            orderDetail.setNumber(cart.getNumber());//数量
            orderDetail.setImage(cart.getImage());//图片
            //调用mapper方法保存
            orderDetailMapper.insert(orderDetail);

            //累加订单金额
            orderAmount = orderAmount.add(cart.getAmount().multiply(new BigDecimal(cart.getNumber())));
        }
        //封装订单
        order.setId(orderId);//订单id
        order.setNumber(orderId.toString()); // 订单号
        order.setStatus(1);//待付款
        order.setUserId(user.getId());//下单用户
        order.setOrderTime(new Date());//下单时间
        order.setCheckoutTime(new Date());//付款时间
        order.setAmount(orderAmount);//订单金额
        order.setUserName(user.getName());//用户名
        order.setPhone(address.getPhone());//用户手机号
        order.setAddress(address.getDetail());//收获地址
        order.setConsignee(address.getConsignee());//收获人姓名
        //保存
        orderMapper.insert(order);
        //清空购物车
        cartService.clean();

    }

    //后台订单查询
    @Override
    public Page<Order> findByPage(Integer pageNum, Integer pageSize, String number, String beginTime, String endTime) {
        LambdaQueryWrapper<Order> qw = new LambdaQueryWrapper<>();
        qw.like(StrUtil.isNotEmpty(number), Order::getNumber, number);//根据传入名称查询用户
        qw.between(StrUtil.isNotEmpty(beginTime), Order::getOrderTime, beginTime, endTime);//根据传入时间查询范围内时间
        qw.between(StrUtil.isNotEmpty(endTime), Order::getOrderTime, beginTime, endTime);//根据传入时间查询范围内时间
        qw.orderByDesc(Order::getOrderTime);//根据最近订单排序
        //构建分页条件
        Page<Order> page = new Page<>(pageNum, pageSize);
        //查询分页
        page = orderMapper.selectPage(page, qw);
        List<Order> orderList = page.getRecords();
        for (Order order : orderList) {
            //查询订单详情
            //为订单详情条件查询创建条件对象
            LambdaQueryWrapper<OrderDetail> oqw = new LambdaQueryWrapper<>();
            //订单对象的id和订单详情的orderid做比较
            oqw.eq(OrderDetail::getOrderId, order.getId());
            //查询订单详情列表
            List<OrderDetail> orderDetailList = orderDetailMapper.selectList(oqw);
            //封装到菜品实体中
            order.setOrderDetails(orderDetailList);
        }
        //返回结果
        return page;
    }

    //h5订单查询
    @Override
    public Page<Order> findByUserPage(Integer pageNum, Integer pageSize, String number) {
        LambdaQueryWrapper<Order> qw = new LambdaQueryWrapper<>();
        qw.like(StrUtil.isNotEmpty(number), Order::getNumber, number);//根据传入名称查询用户
        qw.orderByDesc(Order::getOrderTime);//根据最近订单排序
        //构建分页条件
        Page<Order> page = new Page<>(pageNum, pageSize);
        page = orderMapper.selectPage(page, qw);
        List<Order> orderList = page.getRecords();
        for (Order order : orderList) {
            //查询订单详情
            //为订单详情条件查询创建条件对象
            LambdaQueryWrapper<OrderDetail> oqw = new LambdaQueryWrapper<>();
            //订单对象的id和订单详情的orderid做比较
            oqw.eq(OrderDetail::getOrderId, order.getId());
            //查询订单详情列表
            List<OrderDetail> orderDetailList = orderDetailMapper.selectList(oqw);
            //封装到菜品实体中
            order.setOrderDetails(orderDetailList);
        }
        return page;
    }
}
